<template>
  <div class="container">
    <div class="top-box">
      <div class="left-box">
        <div class="color-box"></div>
        <p>新增文档</p>
      </div>
      <div class="right-box">
        <el-button type="info" icon="el-icon-back" class="btn_fanhui" @click="Return">返回</el-button>
      </div>
    </div>
    <div class="content-box">
      <div class="table-box">
        <div class="item-box">
          <div class="number-box">文档名称</div>
          <div class="number1-box">
            <input type="text" v-model="name" placeholder="请输入文档名称" />
          </div>
        </div>
        <div class="item-box">
          <div class="number-box">文档类型</div>
          <div class="number1-box">
            <input type="text" v-model="typea" placeholder="请输入文档类型" />
          </div>
        </div>
        <div class="item-box">
          <div class="number2-box">文档上传</div>
          <div class="number3-box">
            <el-upload
              class="upload-demo"
              action="/apis/ComApi.asmx/UpFile"
              :on-change="handleChange"
              :on-preview="handlePreview"
              :on-success="function (res, file) { return handleSuccess1(res, file)}"
              :on-remove="function(res,file){ return handleRemove(res,file)}"
              :file-list="fileList"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </div>
        </div>
      </div>
      <div class="btn-box">
        <el-button type="primary" @click="addlist()">确定</el-button>
        <el-button type="info" @click="Return">返回</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  data () {
    return {
      fileList: [],
      newfileList:[],
      equipmentno: "",
      typea: "",
      name: "",
      date: "",
      empno: "",
      newlist: ""
    }
  },
  created () {
    this.empno = JSON.parse(localStorage.getItem('empno'))
  },
  methods: {
    Return () {
      window.history.go(-1);
    },
    handleSuccess1 (response, file) {
      // console.log(response, file)
      // this.fileList=[];
      var datas = this.$x2js.xml2js(response);
      var url = "" + datas.UpFileReturn.fullurl;
      this.newfileList.push({
        status: "success", uid: file.uid, src: url, url: url
      });
      console.log(this.newfileList)
    },
    S4 () { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); },
    guid () { return this.S4() + this.S4() + this.S4() + this.S4(); },
    handleRemove (res, file) {
      let filelist = this.newfileList;
      for (let ind = 0; ind < filelist.length; ind++) {
        const element = filelist[ind];
        if (element.uid == res.uid) { filelist.splice(ind, 1); }
      }
      this.newfileList = filelist;
      console.log(this.newfileList)
    },
    handleChange (file, fileList) {
      this.fileList = fileList.slice(-3);
    },
    handlePreview (file) {
      console.log(file);
    },
    addlist () {
      var obj =
      {
        name: this.name,
        typea: this.typea,
        path: this.newlist.UpFileReturn.url
      }
        ;
      var objs = JSON.stringify(obj);
      console.log(objs)
      axios(
        "/apis/AdministrativeManagement.asmx/Uploaddocument?index=3&jsondata=" + objs + "&empno=" + this.empno
      ).then(({ data }) => {
        var datas = this.$x2js.xml2js(data);
        if (datas.string.__text == "ok") {
          this.$message.success("新增文档成功");
          window.history.go(-1);
        } else {
          this.$message.error(datas.string.__text);
        }
      });
    },
  }
}
</script>
<style lang="scss" scoped>
@import '@/assets/scss/base.scss';
.top-box {
  width: 100%;
  height: 60px;
  overflow: hidden;
  // border: 1px solid $red;
}
.left-box {
  width: 100px;
  height: 40px;
  float: left;
  margin-top: 10px;
  overflow: hidden;
  margin-left: 1%;
  // border: 1px solid red;
}
.color-box {
  width: 5px;
  height: 20px;
  float: left;
  margin-top: 10px;
  background-color: $Header_color;
}
.left-box p {
  float: left;
  margin-left: 5px;
  line-height: 40px;
}
.right-box {
  float: right;
  width: 90px;
  height: 40px;
  margin-right: 2%;
  border-radius: 8px;
  margin-top: 10px;
  background-color: $return-color;
  // border: 1px solid red;
}
.right-box:hover {
  background-color: #a4a6ad;
}
.right-box span {
  float: left;
  color: $white;
  font-size: $font-size-medium;
  line-height: 40px;
  //  border: 1px solid red;
}
.right-box img {
  width: 30px;
  height: 30px;
  margin-top: 5px;
  margin-left: 6px;
  float: left;
  // border: 1px solid red;
}
.content-box {
  width: 98%;
  height: 800px;
  margin: 0 auto;
  border-radius: 8px;
  background-color: $white;
  border: 1px solid $bordercolor;
}
.table-box {
  width: 50%;
  // height: 243px;
  margin: 0 auto;
  margin-top: 200px;
  border-top: 1px solid $bordercolor;
  border-left: 1px solid $bordercolor;
  border-right: 1px solid $bordercolor;
}
.item-box {
  width: 100%;
  // height: 60px;
  display: flex;
  border-bottom: 1px solid $bordercolor;
}
.number-box {
  width: 20%;
  height: 60px;
  line-height: 60px;
  text-align: center;
  color: $blackcolor;
  font-weight: bold;
  font-size: $font-size-medium;
  border-right: 1px solid $bordercolor;
}
.number1-box {
  flex: 1;
  line-height: 60px;
  margin-left: 10%;
}
.number2-box {
  width: 20%;
  height: 120px;
  line-height: 120px;
  text-align: center;
  color: $blackcolor;
  font-weight: bold;
  font-size: $font-size-medium;
  border-right: 1px solid $bordercolor;
}
.number3-box {
  flex: 1;
  line-height: 80px;
  margin-left: 10%;
}
.number1-box input {
  width: 90%;
  height: 60px;
  border: none;
  outline: none;
  font-size: 14px;
  color: #515151;
  margin-left: 10%;
  // text-align: center;
}
.block {
  // text-align: center;
  margin-top: 10px;
  margin-left: 10%;
}
.btn-box {
  width: 20%;
  height: 60px;
  margin: 0 auto;
  padding-left: 100px;
  margin-top: 100px;
  // border: 1px solid red;
}
</style>
